<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.example.domain.Post" %>
<%@ page import="com.example.domain.Comment" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>帖子管理 - 论坛系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #f8fafc 0%, #e0f2fe 100%);
            min-height: 100vh;
            color: #374151;
        }

        .header {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            border-bottom: 1px solid rgba(255, 255, 255, 0.2);
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 16px;
        }

        .header h1 {
            font-size: 28px;
            font-weight: bold;
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .back-btn {
            background: linear-gradient(135deg, #6b7280 0%, #4b5563 100%);
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 25px;
            text-decoration: none;
            font-weight: 600;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .back-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(107, 114, 128, 0.4);
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 30px 20px;
        }

        .stats-section {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .stat-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            padding: 24px;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            text-align: center;
            border: 1px solid rgba(255, 255, 255, 0.2);
            transition: all 0.3s ease;
        }

        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.12);
        }

        .stat-icon {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            margin: 0 auto 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
            color: white;
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
        }

        .stat-number {
            font-size: 24px;
            font-weight: bold;
            background: linear-gradient(135deg, #1f2937 0%, #10b981 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            margin-bottom: 4px;
        }

        .stat-label {
            color: #6b7280;
            font-size: 14px;
            font-weight: 500;
        }

        .section {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            border: 1px solid rgba(255, 255, 255, 0.2);
            margin-bottom: 30px;
        }

        .section-header {
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .section-header h2 {
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 8px;
        }

        .section-header p {
            opacity: 0.9;
            font-size: 16px;
        }

        .table-wrapper {
            overflow-x: auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            padding: 20px 16px;
            text-align: left;
            border-bottom: 1px solid rgba(229, 231, 235, 0.5);
        }

        th {
            background: rgba(248, 250, 252, 0.8);
            font-weight: 600;
            color: #374151;
            font-size: 14px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        td {
            color: #4b5563;
            font-size: 15px;
        }

        tr:hover {
            background: rgba(248, 250, 252, 0.5);
        }

        .status-badge {
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .status-active {
            background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%);
            color: #065f46;
        }

        .status-blocked {
            background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%);
            color: #991b1b;
        }

        .post-title {
            max-width: 300px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            font-weight: 500;
            background: rgba(248, 250, 252, 0.5);
            padding: 8px 12px;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .post-title:hover {
            background: rgba(229, 231, 235, 0.5);
            white-space: normal;
            max-width: none;
        }

        .action-buttons {
            display: flex;
            gap: 8px;
            flex-wrap: wrap;
        }

        .action-btn {
            padding: 10px 16px;
            border: none;
            border-radius: 12px;
            font-size: 13px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }

        .btn-block {
            background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
            color: white;
        }

        .btn-unblock {
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
            color: white;
        }

        .btn-view {
            background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
            color: white;
        }

        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
        }

        .empty-state {
            text-align: center;
            padding: 80px 20px;
            color: #6b7280;
        }

        .empty-state-icon {
            font-size: 64px;
            margin-bottom: 24px;
            opacity: 0.5;
        }

        .empty-state h3 {
            font-size: 24px;
            color: #374151;
            margin-bottom: 12px;
        }

        .empty-state p {
            font-size: 16px;
            line-height: 1.6;
        }

        .id-badge {
            background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
            color: #374151;
            padding: 4px 8px;
            border-radius: 6px;
            font-size: 12px;
            font-weight: 600;
        }

        .author-info {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .author-avatar {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 12px;
            font-weight: 600;
        }

        .post-meta {
            display: flex;
            flex-direction: column;
            gap: 4px;
        }

        .post-date {
            font-size: 12px;
            color: #6b7280;
        }

        .search-section {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            padding: 24px;
            margin-bottom: 30px;
            border: 1px solid rgba(255, 255, 255, 0.2);
        }

        .search-form {
            display: flex;
            gap: 16px;
            align-items: end;
            flex-wrap: wrap;
        }

        .form-group {
            flex: 1;
            min-width: 200px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #374151;
            font-size: 14px;
        }

        .form-input {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e5e7eb;
            border-radius: 12px;
            font-size: 14px;
            transition: all 0.3s ease;
            background: white;
        }

        .form-input:focus {
            outline: none;
            border-color: #10b981;
            box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.1);
        }

        .search-btn {
            padding: 12px 24px;
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
            color: white;
            border: none;
            border-radius: 12px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .search-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(16, 185, 129, 0.4);
        }

        @media (max-width: 768px) {
            .header-content {
                flex-direction: column;
                text-align: center;
            }

            .stats-section {
                grid-template-columns: repeat(2, 1fr);
                gap: 16px;
            }

            .post-title {
                max-width: 200px;
            }

            .action-buttons {
                flex-direction: column;
            }

            .action-btn {
                width: 100%;
                justify-content: center;
            }

            .search-form {
                flex-direction: column;
            }

            .form-group {
                min-width: auto;
            }

            th, td {
                padding: 12px 8px;
                font-size: 13px;
            }
        }

        @media (max-width: 480px) {
            .container {
                padding: 20px 16px;
            }

            .stats-section {
                grid-template-columns: 1fr;
            }

            .section-header {
                padding: 20px;
            }

            .section-header h2 {
                font-size: 20px;
            }

            .search-section {
                padding: 20px;
            }
        }
    </style>
</head>
<body>
<div class="header">
    <div class="header-content">
        <h1>
            <span>📝</span>
            <span>帖子管理</span>
        </h1>
        <a href="/admin" class="back-btn">
            <span>🏠</span>
            <span>返回管理首页</span>
        </a>
    </div>
</div>

<div class="container">
    <%
        List<Post> posts = (List<Post>) request.getAttribute("posts");
        int totalPosts = posts != null ? posts.size() : 0;
        int activePosts = 0;
        int blockedPosts = 0;

        if (posts != null) {
            for (Post post : posts) {
                if ("active".equals(post.getStatus())) activePosts++;
                if ("blocked".equals(post.getStatus())) blockedPosts++;
            }
        }
    %>

    <!-- 统计卡片 -->
    <div class="stats-section">
        <div class="stat-card">
            <div class="stat-icon">📝</div>
            <div class="stat-number"><%= totalPosts %></div>
            <div class="stat-label">总帖子数</div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">✅</div>
            <div class="stat-number"><%= activePosts %></div>
            <div class="stat-label">正常帖子</div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">🚫</div>
            <div class="stat-number"><%= blockedPosts %></div>
            <div class="stat-label">屏蔽帖子</div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">📊</div>
            <div class="stat-number"><%= totalPosts > 0 ? Math.round((double)activePosts / totalPosts * 100) : 0 %>%</div>
            <div class="stat-label">健康度</div>
        </div>
    </div>

    <!-- 搜索筛选 -->
    <div class="search-section">
        <form class="search-form" method="get" action="/admin/posts">
            <div class="form-group">
                <label>搜索标题</label>
                <input type="text" name="keyword" class="form-input" placeholder="输入帖子标题关键词...">
            </div>
            <div class="form-group">
                <label>状态筛选</label>
                <select name="status" class="form-input">
                    <option value="">全部状态</option>
                    <option value="active">正常</option>
                    <option value="blocked">屏蔽</option>
                </select>
            </div>
            <button type="submit" class="search-btn">
                <span>🔍</span>
                <span>搜索</span>
            </button>
        </form>
    </div>

    <!-- 帖子管理部分 -->
    <div class="section">
        <div class="section-header">
            <h2>帖子列表</h2>
            <p>管理所有论坛帖子，维护内容质量</p>
        </div>

        <% if (posts != null && !posts.isEmpty()) { %>
        <div class="table-wrapper">
            <table>
                <thead>
                <tr>
                    <th>ID</th>
                    <th>标题</th>
                    <th>作者</th>
                    <th>发布时间</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <% for (Post post : posts) { %>
                <tr>
                    <td><span class="id-badge"><%= post.getId() %></span></td>
                    <td>
                        <div class="post-title" title="<%= post.getTitle() %>">
                            <%= post.getTitle() %>
                        </div>
                    </td>
                    <td>
                        <div class="author-info">
                            <div class="author-avatar">
                                <%= post.getUsername() != null ? post.getUsername().substring(0, 1).toUpperCase() : "U" %>
                            </div>
                            <div class="post-meta">
                                <div><%= post.getUsername() != null ? post.getUsername() : "用户" + post.getUserId() %></div>
                                <div class="id-badge">ID: <%= post.getUserId() %></div>
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="post-date"><%= post.getCreateTime() %></div>
                    </td>
                    <td>
                        <span class="status-badge status-<%= post.getStatus() %>">
                            <%= "active".equals(post.getStatus()) ? "正常" : "屏蔽" %>
                        </span>
                    </td>
                    <td>
                        <div class="action-buttons">
                            <a href="/postDetail?id=<%= post.getId() %>" class="action-btn btn-view" target="_blank">
                                <span>👁️</span>
                                <span>查看</span>
                            </a>
                            <form action="/admin/updatePostStatus" method="post" style="display:inline;">
                                <input type="hidden" name="id" value="<%= post.getId() %>"/>
                                <input type="hidden" name="status" value="<%= post.getStatus().equals("active") ? "blocked" : "active" %>"/>
                                <button type="submit" class="action-btn <%= post.getStatus().equals("active") ? "btn-block" : "btn-unblock" %>">
                                    <span><%= post.getStatus().equals("active") ? "🚫" : "✅" %></span>
                                    <span><%= post.getStatus().equals("active") ? "屏蔽" : "解除屏蔽" %></span>
                                </button>
                            </form>
                        </div>
                    </td>
                </tr>
                <% } %>
                </tbody>
            </table>
        </div>
        <% } else { %>
        <div class="empty-state">
            <div class="empty-state-icon">📝</div>
            <h3>暂无帖子数据</h3>
            <p>论坛中还没有发布的帖子</p>
        </div>
        <% } %>
    </div>
</div>
</body>
</html>
